package com.rede.didiok.sms.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rede.didiok.common.domainEvent.DomainEvent;
import com.rede.didiok.common.domainEvent.EntityType;
import com.rede.didiok.common.dto.rank.MemberCommentDto;
import com.rede.didiok.common.dto.user.UserDto;
import com.rede.didiok.common.enums.AuditStatusEnum;
import com.rede.didiok.common.exception.exceptionType.FeignException;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.sms.event.annotation.EventType;
import com.rede.didiok.sms.event.service.AbstractEventHandler;

/**
 * 评论事件处理器
 */
@Slf4j
@Component
@EventType({EntityType.MEMBER_COMMENT})
public class MemberCommentEventHandler extends AbstractEventHandler {

    @Override
    public void doEventHandler(DomainEvent domainEvent) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MemberCommentDto comment = objectMapper.convertValue(domainEvent.getEntity(), MemberCommentDto.class);
        switch (domainEvent.getEventAction()) {
            case MEMBER_COMMENT_ADD: {
                this.add(comment, domainEvent.isAdminOperate());
            }
            break;
            case MEMBER_COMMENT_DELETE: {
                this.delete(comment, domainEvent.isAdminOperate());
            }
            break;
            case MEMBER_COMMENT_AUDIT: {
                this.audit(comment, domainEvent.isAdminOperate());
            }
            break;
        }
    }

    /**
     * 审核评论
     *
     * @param comment
     * @param isAdminOperate
     */
    public void audit(MemberCommentDto comment, boolean isAdminOperate) {

        /**
         * 发送邮件
         */
        if (StringUtils.isNotEmpty(comment.getCreateUserUid())) {
            R result = userFeignClient.getUserById(comment.getCreateUserUid());
            UserDto user;
            if(result.getCode()==0){
                user = result.getData(new TypeReference<UserDto>(){});
            }else {
                // 获取异常信息
                throw new FeignException("feign调用异常（根据用户id找用户）：失败");
            }
            StringBuilder text = new StringBuilder();
            text.append(String.format("<p>可爱的博主%s,您的评论</p>", user.getNickname()));
            if (AuditStatusEnum.REJECT.equals(comment.getAuditStatus())) {
                text.append("审核未通过,审核原因为" + comment.getRejectReason());
                rabbitMqUtil.sendSimpleEmail(user.getEmail(), text.toString());
            } else if (AuditStatusEnum.AGREE.equals(comment.getAuditStatus())) {
                text.append("审核已通过");
                rabbitMqUtil.sendSimpleEmail(user.getEmail(), text.toString());
            } else {
                log.error("审核状态异常,稿件为{}", comment.getUid());
            }
        }
    }

    /**
     * 添加评论
     *
     * @param comment
     * @param isAdminOperate
     */
    public void add(MemberCommentDto comment, boolean isAdminOperate) {

        // 判断是否是评论的机器人【若机器人会触发主动回复】

        String atUserUid = StringUtils.getAtUserUid(comment.getContent());

        // 判断是否触发AT功能，触发AT后的动作
        if (StringUtils.isNotEmpty(atUserUid)) {
            atEventHandler.atEventHandler(comment.getCreateUserUid(), atUserUid, comment.getUid(), comment.getMemberUid(), comment.getSource(), comment.getContent(), true);
        } else if (StringUtils.isNotEmpty(comment.getToUserUid())) {
            atEventHandler.atEventHandler(comment.getCreateUserUid(), comment.getToUserUid(), comment.getUid(), comment.getMemberUid(), comment.getSource(), comment.getContent(), false);
        }

        // 通过榜单成员评论发送通知
        userFeignClient.sendNoticeByMemberComment(comment);
    }

    /**
     * 添加评论
     *
     * @param comment
     * @param isAdminOperate
     */
    public void delete(MemberCommentDto comment, boolean isAdminOperate) {
//        if (StringUtils.isNotEmpty(comment.getUserUid()) && AuditStatusEnum.AGREE.equals(comment.getAuditStatus())) {
//            // 评论删除，扣除积分
//            userService.addUserCredits(ECreditType.Delete_Comment.getAction(), null, comment.getUid(), comment.getUserUid());
//        }
    }
}
